package com.ruoyi.web.controller.community.owner;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.BaseActivityRegisterRequest;
import com.ruoyi.community.service.IOwnerActivityEnrollService;
import com.ruoyi.community.service.IOwnerActivityParticipateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.OwnerActivity;
import com.ruoyi.community.service.IOwnerActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业委会活动Controller
 * 
 * @author ruoyi
 * @date 2021-08-28
 */
@RestController
@RequestMapping("/community/owner/activity")
public class OwnerActivityController extends BaseController
{
    @Autowired
    private IOwnerActivityService ownerActivityService;

    @Autowired
    private IOwnerActivityEnrollService ownerActivityEnrollService;

    @Autowired
    private IOwnerActivityParticipateService ownerActivityParticipateService;

    /**
     * 查询业委会活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(OwnerActivity ownerActivity)
    {
        startPage();
        List<OwnerActivity> list = ownerActivityService.selectOwnerActivityList(ownerActivity);
        return getDataTable(list);
    }

    /**
     * 导出业委会活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:export')")
    @Log(title = "业委会活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OwnerActivity ownerActivity)
    {
        List<OwnerActivity> list = ownerActivityService.selectOwnerActivityList(ownerActivity);
        ExcelUtil<OwnerActivity> util = new ExcelUtil<OwnerActivity>(OwnerActivity.class);
        return util.exportExcel(list, "业委会活动数据");
    }

    /**
     * 获取业委会活动详细信息
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:query')")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") String activityId)
    {
        return AjaxResult.success(ownerActivityService.selectOwnerActivityById(activityId));
    }

    /**
     * 新增业委会活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:add')")
    @Log(title = "业委会活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OwnerActivity ownerActivity)
    {
        return toAjax(ownerActivityService.insertOwnerActivity(ownerActivity));
    }

    /**
     * 修改业委会活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:edit')")
    @Log(title = "业委会活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OwnerActivity ownerActivity)
    {
        return toAjax(ownerActivityService.updateOwnerActivity(ownerActivity));
    }

    /**
     * 删除业委会活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:remove')")
    @Log(title = "业委会活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable String[] activityIds)
    {
        return toAjax(ownerActivityService.deleteOwnerActivityByIds(activityIds));
    }

    /**
     * 查询是否已签到
     */
    @PostMapping("/state")
    public int ifEnrolledOrRegistered(@RequestBody BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        baseActivityRegisterRequest.setUserId(loginUser.getUserId());
        if(!ownerActivityEnrollService.hasEnrolled(baseActivityRegisterRequest)){
            return 0;
        }else if(!ownerActivityParticipateService.hasRegistered(baseActivityRegisterRequest)){
            return 1;
        }else {
            return 2;
        }
    }

    /**
     * 查询用户历史记录,包括投票、报名业委会活动和参与业委会活动
     */
    @GetMapping("/me")
    public AjaxResult getMyRecord()
    {
        return AjaxResult.success(ownerActivityService.getMyRecord());
    }
}
